package cn.edu.scut.sys.controller;

import java.sql.Timestamp;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.scut.sys.service.LeaderService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.utils.StringUtils;
import cn.edu.scut.sys.vo.Leader;
import cn.edu.scut.sys.vo.User;

@Controller
@RequestMapping("/leader")
public class LeaderController {

	private LeaderService leaderService;

	@Resource(name="leaderService")
	public void setLeaderService(LeaderService leaderService) {
		this.leaderService = leaderService;
	}
	
	/**
	 * 
	 * @param page
	 * @param rows
	 * @param sidx
	 * @param sord
	 * @param search
	 * @param searchField
	 * @param searchOper
	 * @param searchString
	 * @param filters
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public DataResponse<Leader> list(@RequestParam(defaultValue="1",value="page") String page,  
            @RequestParam(defaultValue="20",value="rows") String rows,  
            @RequestParam("sidx") String sidx,  
            @RequestParam("sord") String sord,  
            @RequestParam("search") boolean search,  
            @RequestParam(required=false,value="searchField") String searchField,  
            @RequestParam(required=false,value="searchOper") String searchOper,  
            @RequestParam(required=false,value="searchString") String searchString,  
            @RequestParam(required=false,value="filters") String filters  
            ){  
		 try {  
	            DataRequest request = new DataRequest();  
	            request.setPage(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));  
	            request.setRows(StringUtils.isEmpty(rows) ? 20 : Integer.valueOf(rows));  
	            request.setSidx(sidx);  
	            request.setSord(sord);  
	            request.setSearch(search);  
	            request.setSearchField(searchField);  
	            request.setSearchOper(searchOper);  
	            request.setSearchString(searchString);  
	            return leaderService.list(request);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	        return null;  
	}
	
	@RequestMapping("/add")
	public String add(String name, String registerTime, String sex, String level, String status, HttpServletRequest request){
		try{
			Leader leader = new Leader();
			leader.setName(name);
			leader.setRegisterTime(Timestamp.valueOf(registerTime + " 00:00:00"));
			leader.setSex(Integer.parseInt(sex));
			leader.setLevel(Integer.parseInt(level));
			leader.setStatus(Integer.parseInt(status));
			leaderService.add(leader);
			request.setAttribute("status", 1);
			return "/page/leaderAdd";
		} catch(Exception ex) {
			request.setAttribute("status", 0);
			return "/page/leaderAdd";
		}
	}
	
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable(value = "id") Long id, HttpServletRequest request){
		request.setAttribute("leader", leaderService.get(id));
		return "/page/leaderEdit";
	}
	
	@RequestMapping("/update")
	public String update(String id, String name, String registerTime, String sex, String level, String status, HttpServletRequest request){
		try{
			Leader leader = leaderService.get(Long.parseLong(id));
			leader.setId(Long.parseLong(id));
			leader.setName(name);
			leader.setRegisterTime(Timestamp.valueOf(registerTime + " 00:00:00"));
			leader.setSex(Integer.parseInt(sex));
			leader.setLevel(Integer.parseInt(level));
			leader.setStatus(Integer.parseInt(status));
			leaderService.update(leader);
			request.setAttribute("status", 1);
			return "/page/leaderEdit";
		} catch(Exception ex) {
			request.setAttribute("status", 0);
			return "/page/leaderEdit";
		}
	}
	
	@RequestMapping("/setUserPage/{id}")
	public String setUserPage(@PathVariable(value = "id") Long id, HttpServletRequest request){
		request.setAttribute("leader", leaderService.get(id));
		return "/page/leaderUserList";
	}
	
	@RequestMapping("/setUser")
	public String setUser(Long userId, Long leaderId, HttpServletRequest request){
		try{
			Leader leader = leaderService.get(leaderId);
			User user = new User();
			user.setId(userId);
			leader.setUser(user);
			leaderService.update(leader);
			return "/page/leaderList";
		}catch(Exception ex){
			return "/page/leaderList";
		}
	}
	
	@RequestMapping("/setUserNull")
	public String setUserNull(Long leaderId, HttpServletRequest request){
		try{
			Leader leader = leaderService.get(leaderId);
			leader.setUser(null);
			leaderService.update(leader);
			return "/page/leaderList";
		}catch(Exception ex){
			return "/page/leaderList";
		}
	}
}
